package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.UserMessage
import me.leiusual.sharing.repository.UserMessageRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.observeOnce
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class MineMessageViewModel @Inject constructor(
    private val userMessageRepository: UserMessageRepository
) : ViewModel() {

  // 消息列表

  private val _userMessageQuery = MutableLiveData<Unit>()
  private val _listing: LiveData<UserMessageRepository.UserMessageQueryListing> = map(_userMessageQuery) {
    userMessageRepository.userMessageQuery()
  }
  val loadInitial: LiveData<Data<Unit>> = switchMap(_listing) {
    it.loadInitial
  }
  val userMessageQuery: LiveData<PagedList<UserMessage>> = switchMap(_listing) {
    it.pagedList
  }

  fun userMessageQuery() {
    _userMessageQuery.value = Unit
  }

  // 消息已读

  fun userMessageView(id: Long) {
    userMessageRepository.userMessageView(id).toLiveData().observeOnce()
  }
}